import os
from collections import deque

lists = [r"G:\备份\扫叶提供维普数据2023.08\04-2史類",
         r"G:\备份\扫叶提供维普数据2023.08\04-3子類",
         r"G:\备份\扫叶提供维普数据2023.08\04-1經類",
         r"G:\备份\扫叶提供维普数据2023.08\04-4集類"]
my_deque = deque(maxlen=2)
my_deque2 = deque(maxlen=6)
for path in lists:
    for path, dirNames, fileNames in os.walk(path, topdown=True):
        # 获取当前目录的所有文件
        for fileName in fileNames:
            is_print = 0
            if not fileName.find("txt") > -1:
                continue
            file_path = os.path.join(path, fileName)
            print(file_path)
            try:
                with open(file_path, "r", encoding='utf-8') as file:
                    for line in file:
                        if line.startswith("\ufeff"):
                            line = line.replace("\ufeff", "")
                        if line.startswith('Ｃ4'):
                            for line2 in my_deque:
                                my_deque2.append(line2)
                            my_deque2.append(line)
                            is_print = 3
                        elif is_print > 0:
                            my_deque2.append(line)
                            is_print = is_print - 1
                        else:
                            if my_deque2:
                                print(my_deque2)
                                my_deque2.clear()
                            my_deque.append(line)

            except:
                print("err:" + file_path)

# from collections import deque
#
# # 创建一个最大长度为2的双端队列
# my_deque = deque(maxlen=2)
#
# # 添加新数据
# my_deque.append("新数据1")
# my_deque.append("新数据2")
# my_deque.append("新数据3")
#
# for line in my_deque:
#     print(line)
# # 打印队列
# print(my_deque)
